package com.mjk.common.tools.gson.adapter.impl;

import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import com.mjk.common.tools.logger.BizLogger;
import io.seata.common.util.BlobUtils;

import javax.sql.rowset.serial.SerialBlob;
import javax.sql.rowset.serial.SerialClob;
import java.io.IOException;
import java.sql.Blob;

public class BlobAdapter extends TypeAdapter<Blob> {

    @Override
    public void write(JsonWriter jsonWriter, Blob blob) throws IOException {
        jsonWriter.value(BlobUtils.blob2string(blob));
    }

    @Override
    public Blob read(JsonReader jsonReader) throws IOException {
        try {
            return new SerialBlob(jsonReader.nextString().getBytes());
        } catch (Exception e) {
            BizLogger.error(e);
        }
        return null;
    }
}
